package com.longtop.framework.sys.login.web;

import javax.servlet.http.HttpServletRequest;

import com.longtop.framework.common.Constant;
import com.longtop.framework.common.Message;
import com.longtop.framework.common.ResourceConstants;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.sys.log.service.LogService;
import com.longtop.framework.sys.login.service.LoginService;
import com.longtop.framework.util.SpringBeanFactory;
import com.longtop.framework.util.StringUtil;
import com.longtop.intelliweb.action.AbstractBaseAction;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;

/**
 * 
 * @author 孙景弘
 * 
 */
public class LoginAction extends AbstractBaseAction {

	/**
	 * 
	 */
	public void init(IInitContext context) {

	}

	/**
	 * 用户登陆系统时进行用户名、密码校验
	 * 
	 * @param context
	 *            intelliWeb context上下文信息
	 */
	public void loginCheck(IActionContext context) {

		// 1.取得用户输入的登陆信息
		String loginName = context.getElementValue("Editor_loginName").trim();
		String password = context.getElementValue("Editor_password").trim();
		String loginType = StringUtil.getString(context.getCommandParameterValue("loginType"));

		SessionObject sessionObject = new SessionObject();
		HttpServletRequest request = context.getRequest();
		LogService logService = SpringBeanFactory.getBean(LogService.BEAN_ID);

		// 从Spring容器取到Service类
		LoginService loginService = SpringBeanFactory.getBean(LoginService.BEAN_ID);

		// 1.以下是用户用户信息合法性检查
		try {
			sessionObject = loginService.loginCheck(loginName, password, loginType, request);
		} catch (BaseCheckedException e) {
			// 登陆失败写入日志
			logService.saveLog(Constant.BIZ_TYPE_001, Constant.LOG_TYPE_01, Message.getMessage(ResourceConstants.W_LOGIN_0004, new Object[] { loginName }) + ";" + e.getMessage(), loginName, "", context.getRequest().getRemoteAddr());

			this.hintErrorMessage(e.getMessage());
			return;
		}
		// 登陆成功写入日志
		logService.saveLog(Constant.BIZ_TYPE_001, Constant.LOG_TYPE_01, Message.getMessage(ResourceConstants.W_LOGIN_0005, new Object[] { loginName }), loginName, sessionObject.getUserName(), context.getRequest().getRemoteAddr());

		// 5.把取得的信息写入session
		if (request.getSession().getAttribute(Constant.SESSION_OBJECT) != null) {
			request.getSession().removeAttribute(Constant.SESSION_OBJECT);
			request.getSession().invalidate();
		}
		request.getSession(true).setAttribute(Constant.SESSION_OBJECT, sessionObject);

		// findForward("Forward_success"); // 跳转到主界面
	}

	/**
	 * 首页切换角色
	 * 
	 * @param context
	 */
	public void changeRole(IActionContext context) {

		String roleId = context.getCommandParameterValue("roleId");

		HttpServletRequest request = context.getRequest();
		SessionObject sessionObject = (SessionObject) request.getSession().getAttribute(Constant.SESSION_OBJECT); // 先取得老sessionObject

		LoginService loginService = SpringBeanFactory.getBean(LoginService.BEAN_ID);
		// 1.以下是用户用户信息合法性检查
		try {
			sessionObject = loginService.changeRole(sessionObject, request, roleId);
		} catch (Exception e) {
			e.printStackTrace();
			this.hintErrorMessage(e.getMessage());
			return;
		}
		// 2.把取得的信息写入session
		if (request.getSession().getAttribute(Constant.SESSION_OBJECT) != null) {
			request.getSession().removeAttribute(Constant.SESSION_OBJECT);
		}
		request.getSession(true).setAttribute(Constant.SESSION_OBJECT, sessionObject);

		// findForward("Forward_success"); // 跳转到主界面
	}
}
